package com.example.home.bus.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import androidx.fragment.app.Fragment;

import com.example.home.R;
import com.example.home.bus.activity.MakeNext1;

public class MakeFragment extends Fragment {

    private EditText start;
    private EditText end;
    private EditText path;
    private EditText price;
    private Button btnNext;
    private View view;
    private MFBroadcastReceiver mfBroadcastReceiver;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_make, container, false);
        initView();
        setupNextButton();
        mfBroadcastReceiver = new MFBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("bov_action3");
        MakeFragment.this.getActivity().registerReceiver(mfBroadcastReceiver,filter);
        return view;
    }

    private void setupNextButton() {
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String star = start.getText().toString();
                String en = end.getText().toString();
                String pric = price.getText().toString();
                String pat = path.getText().toString();

                if (!TextUtils.isEmpty(star)) {
                    Intent intent = new Intent(getActivity(), MakeNext1.class);
                    intent.putExtra("start", star);
                    intent.putExtra("end", en);
                    intent.putExtra("price", pric);
                    intent.putExtra("path", pat);
                    getActivity().startActivity(intent);
                }
            }
        });

        // Add TextWatcher to the EditText fields
        start.addTextChangedListener(watcher);
        end.addTextChangedListener(watcher);
        path.addTextChangedListener(watcher);
        price.addTextChangedListener(watcher);
    }

    // TextWatcher to track text changes in EditText fields
    private TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            // Check if any of the EditText fields has content
            boolean hasContent = !TextUtils.isEmpty(start.getText())
                    || !TextUtils.isEmpty(end.getText())
                    || !TextUtils.isEmpty(price.getText())
                    || !TextUtils.isEmpty(path.getText());

            // Update the visibility of the btnNext button
            btnNext.setVisibility(hasContent ? View.VISIBLE : View.INVISIBLE);
        }
    };

    private void initView() {
        start = view.findViewById(R.id.start);
        end = view.findViewById(R.id.end);
        path = view.findViewById(R.id.path);
        price = view.findViewById(R.id.price);
        btnNext = view.findViewById(R.id.btn_next);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        MakeFragment.this.getActivity().unregisterReceiver(mfBroadcastReceiver);
    }

    public class MFBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ("bov_action3".equals(action)){
                start.setText("");
                end.setText("");
                path.setText("");
                price.setText("");

                price.clearFocus();
                start.clearFocus();
                end.clearFocus();
                path.clearFocus();
            }
        }
    }
}
